package com.joey.concurrency.example.commUnsafe;

import com.joey.concurrency.annotation.NotThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Vector;

/**
 * @Author hyy
 **/
@Slf4j
@NotThreadSafe
public class VectorExample {

    private static Vector<Integer> vector = new Vector<Integer>();

    public static void main(String[] args) {
        while(true){
            for (int i =0;i < 10; i++){
                vector.add(i);
            }
            //操作顺序的差异引起线程不安全
            Thread thread = new Thread(){
                public void run(){
                    for(int i = 0;i<vector.size();i++){
                        vector.remove(i);
                    }
                }
            };
            Thread thread2 = new Thread(){
                public void run(){
                    for(int i = 0;i<vector.size();i++){
                        vector.add(i);
                    }
                }
            };
            thread.start();
            thread2.start();
        }
    }


}
